from ..models import RoleModel, PermissionsModel
from rest_framework.serializers import ModelSerializer, IntegerField, BooleanField, Serializer

from .permission import PermissionsSerializer


class BaseRolesSerializer(ModelSerializer):
    """
    普通序列化类： 支持新增，修改角色名，删除，查询
    """
    permissions = PermissionsSerializer(many=True, read_only=True)

    class Meta:
        model = RoleModel
        fields = "__all__"


class RolesPartialSerializer(ModelSerializer):
    """
    用于给某一个角色批量授权的序列化
    """

    class Meta:
        model = RoleModel
        fields = ["id", "permissions"]


class RoleSetPermissionsSerializer(Serializer):
    """
    用于给某一个角色单一授权也包括取消单一授权
    """
    # 角色ID
    role_id = IntegerField(write_only=True, required=True)
    # 权限ID
    permission_id = IntegerField(write_only=True, required=True)
    # 是否新授予权限， 还是取消
    is_create = BooleanField(write_only=True, required=True)
